package br.com.caelum.dao;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.caelum.model.Matricula;
import br.com.caelum.vraptor.ioc.Component;

@Component
public class MatriculaDAO {
	private Session session;
	
	public MatriculaDAO(Session session) {
		this.session = session;
	}
	
	public void salva(Matricula matricula) {
		Transaction tx = session.beginTransaction();
		session.save(matricula);
		tx.commit();
	}
	
	public void apaga(Matricula matricula) {
		Transaction tx = session.beginTransaction();
		session.delete(matricula);
		tx.commit();
	}
	
	public void atualiza(Matricula matricula) {
		Transaction tx = session.beginTransaction();
		session.update(matricula);
		tx.commit();
	}
	
	public Matricula carrega(Long id) {
		return (Matricula) this.session.load(Matricula.class, id);
	}
	
	public List<Matricula> listaTudo() {
		return this.session.createCriteria(Matricula.class).list();
	}
	
	public List<Matricula> listaPorAluno(Long id) {
		return this.session.createCriteria(Matricula.class)
				.add(Restrictions.eq("aluno.aluno_id", id))
				.list();
	}
	
	public List<Matricula> listaPorTurma(Long id) {
		return this.session.createCriteria(Matricula.class)
				.add(Restrictions.eq("turma.turma_id", id))
				.list();
	}

}

